<%-- 
    Document   : index
    Created on : 12.04.2013, 23:06:53
    Author     : Yasya
--%>


<%@page import="delta.productcatalog.model.Category"%>
<%@page import="delta.productcatalog.controller.CategoryFacadeRemote"%>
<%@page import="delta.productcatalog.model.Product"%>
<%@page import="delta.productcatalog.controller.ProductManager"%>
<%@page import="javax.naming.NamingException"%>
<%@page import="javax.naming.InitialContext"%>
<%@page import="delta.productcatalog.controller.ProductManagerRemote"%>
<%@page contentType="text/html" pageEncoding="UTF-8"%>

<%!    private ProductManagerRemote pm;
private CategoryFacadeRemote categoryFacade;
    
    public void jspInit() {
        try {
            InitialContext ic = new InitialContext();
            pm = (ProductManagerRemote) ic.lookup(
                "java:global/ProductCatalog/ProductCatalog-ejb/ProductManager!delta.productcatalog.controller.ProductManagerRemote");
        categoryFacade=(CategoryFacadeRemote) ic.lookup(
                "java:global/ProductCatalog/ProductCatalog-ejb/CategoryFacade!delta.productcatalog.controller.CategoryFacadeRemote");
        
        } catch (NamingException ex) {
            ex.printStackTrace();
        }
    }
%>

<script>
function deleteProduct(){
    document.getElementById("mainForm").action="DeleteProduct";
    document.getElementById("mainForm").submit();
}

function editProduct(){
    document.getElementById("mainForm").action="edit_product.jsp";
    document.getElementById("mainForm").submit();
}


</script>
<!DOCTYPE html>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <link rel="stylesheet" type="text/css" href="css/styl.css">
        <title>Work with product</title>
    </head>
    <body>
        <header>
            <div id ="top_name"> <h1> Warehouse employee</h1> </div>
            <div id="user-panel">
                <p> <a href="#">User name</a>
;                    <a href="#">LogOut</a> </p>
            </div>
            <div id="menu">
                <div id="line"> <a href="#"> Orders</a> </div>
                <div id="line"> <a href="#"> Warehouses</a> </div>
                <div id="line"> <a href="#"> Product</a> </div>

                <div id="role"> <select name="role" size = 1> 
                        <option value=”1” >Customer</option>
                        <option value=”2”>Warehouse Employee</option> 
                        <option value=”3”>Courier</option> 
                        <option value=”4”, selected>Admin</option> 
                    </select> </div>
            </div>
        </header>
        <div id="main">
            <div id="find">  
                <form method="POST" action=""> 
                    <p>Find:
                        <input type="text" name="Find" size="30" value="keyword" id="label"/>
                        <button id="go" type="submit" name="go"  >Go</button> </p>
                </form>
            </div>
            <div id="export"> <form method="POST" action="">  <button type="submit" name="export" >Export data</button></form>
            </div>
            <div id="sorted">
                <p> Category: <select  name="category" size = 1>
                        
                        <%for(Category c: categoryFacade.findAll()){ %>
                        <option value=<%=c.getId_category()%> > <%= c.getName()%></option>
                        <%}%>
                    </select> 
                    <a href="change_category.jsp"> Create/delete category </a> </div> </p>
        <div id="sorted"> <p> Sort by:  &nbsp   <select name="sort"  size = 1> 
                    <option value=”11” >price</option>
                    <option value=”22”, selected>name</option> </p>
            </select> 
        </div>
        <div id="tabl">
            <form method="Get" id="mainForm" > 
                
       
            <table>
                <TR>
                     <td id="head_table_zero"></td>
                    <td id="head_table_first"><strong>Name</strong></td>
                    <td id="head_table_second"><strong>Price</strong></td>
                    <td id="head_table_third"><strong>Description</strong></td>
                </TR>
             
                <% 
                int pageNumber;
                String pageNum=request.getParameter("pageNumber");
                if(pageNum!=null){
                pageNumber=Integer.parseInt(pageNum);
                } else{
                pageNumber=1;
                }
                int maxResults=10;
                int firstResult=maxResults*(pageNumber-1);

for(Product p: pm.findRange(firstResult, maxResults)){ %>
                

                <TR>
                    <td> <input type="checkbox" name="id_product" value=<%=p.getId_product() %>></td>
                  
                    <td id="colomn_first" ><%=p.getName() %> </td>
                    <td id="colomn_second"><%=p.getPrice() %> </td>
                    <td id="colomn_third"> <%=p.getDescription() %> </td>
                </TR>
      <%  } %>

            </table>
      
           </form>
        </div>
        <div id="line_button"> <button id="function" onclick="window.location.href='add_product.jsp' ">Add product</button></div>
        <div id="line_button"> <button id="function" onclick="editProduct()">Edit product</button></div>
        <div id="line_button"> <button id="function" onclick="deleteProduct()">Delete product</button> </div>
        <div id="line_button"> <button id="navigation" name="back" onclick="window.location.href='index.jsp?pageNumber=<%= pageNumber>1?pageNumber-1:1%>' "> << </button></div>
        <div id="line_button"> 
            <button 
                id="navigation" 
                name="forward" 
                onclick="window.location.href='index.jsp?pageNumber=<%=pageNumber+1 %>' "> 
                >> 
            </button>
        </div>
    </div>
</body>
</html>